﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class MailInbox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.MailInbox_PageTitle;
            this.InfoLabel.ForeColor = System.Drawing.Color.Red;
            if (Request.QueryString["ConfirmMessage"] != null)
                this.InfoLabel.Text = Request.QueryString["ConfirmMessage"].ToString();

            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                
                if (ou.TeamId == "Null")
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

                if (!Page.IsPostBack)
                    this.SqlDataSource1.SelectParameters.Add("UserId", ou.UserId);
                //Response.Write(NewMailsCounter(UsrId));
                int NumOfUnreadMails = (NewMailsCounter(ou.UserId));
                this.NumOfMailsLabel.Text = (Resources.Resource.MailInbox_NumOfMailsLabelLeft + " " + NumOfUnreadMails + " " + Resources.Resource.MailInbox_NumOfMailsLabelRight);
            }

        }


        protected int NewMailsCounter(String UsrId)
        {

            string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection
            strConnection = new System.Data.SqlClient.SqlConnection(conn);
            string StrSQL = "Nivdal_MailGetNumOfNewMails";
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UsrId));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ROWCOUNT", System.Data.SqlDbType.Int, 0));
            cmd.Parameters["@ROWCOUNT"].Direction = ParameterDirection.ReturnValue;

            strConnection.Open();
            try
            {

                cmd.ExecuteNonQuery();
                return (int)(cmd.Parameters["@ROWCOUNT"].Value);

            }
            catch (Exception SQLDBException)
            {
                Response.Write(SQLDBException.Message);
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                return 0;

            }

            finally
            {

                strConnection.Close();
            }

        }

        protected String DeleteAMail(String UsrId, String MailId)
        {

            string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection
            strConnection = new System.Data.SqlClient.SqlConnection(conn);
            string StrSQL = "Nivdal_MailDeleteMail";
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UsrId));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@MailId", MailId));
            //cmd.Parameters["@Answer"].Direction = ParameterDirection.ReturnValue;

            strConnection.Open();
            try
            {

                cmd.ExecuteNonQuery();
                //return (String)(cmd.Parameters["@Answer"].Value);
                return (Resources.Resource.MailInbox_ConfirmDeletedMail);

            }
            catch (Exception SQLDBException)
            {
                this.InfoLabel.Text = SQLDBException.Message;
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                return (Resources.Resource.MailInbox_NotDeletedMailNote);

            }

            finally
            {

                strConnection.Close();
            }

        }

        protected void Delete(Object sender, GridViewCommandEventArgs e)
        {
            int MailId = 0;
            // get the row index stored in the CommandArgument property
            int.TryParse(e.CommandArgument as string, out MailId);

            if (MailId == 0)
            {
                this.InfoLabel.Text = Resources.Resource.MailInbox_AccessProviderProblem;
                return;
            }

            if (e.CommandName == "DeleteMail")
            {
                ///int MailId = Convert.ToInt32(e.CommandArgument);
                OnlineUser ou = (OnlineUser)Session["User"];
                //this.RedInfoLabel.Text = DeleteAMail(UsrId, DeleteMailId);
                String DeleteMailConfirm = DeleteAMail(ou.UserId, MailId.ToString());
                Response.Redirect("~/MailInbox.aspx?ConfirmMessage=" + DeleteMailConfirm);
                ///this.InfoLabel.Text = MailId.ToString();
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void DeleteAll(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string MailId;

            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.Visible)
                {
                    MailId = (row.FindControl("MailIdHiddenField") as HiddenField).Value;
                    GND.ExecNivdalSQL("MailId", "UserId", "Nivdal_MailDeleteMail", MailId, ou.UserId);
                }
            }

            SqlDataSource1.DataBind();
            GridView1.DataBind();

        }

        protected void DeleteSelected(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string MailId;
            bool Checked;
            foreach (GridViewRow row in GridView1.Rows)
            {
                Checked = (row.FindControl("DeleteCheckBox") as CheckBox).Checked;
                if (Checked)
                {
                    MailId = (row.FindControl("MailIdHiddenField") as HiddenField).Value;
                    GND.ExecNivdalSQL("MailId", "UserId", "Nivdal_MailDeleteMail", MailId, ou.UserId);
                }
            }
            SqlDataSource1.DataBind();
            GridView1.DataBind();

        }

    }
}